package com.zhaoyue.study.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 2017/2/4
 * Time: 17:13
 * To change this template use File | Settings | File Templates.
 */
public class StudentProxy implements InvocationHandler {

    private Object targetObject;

    public StudentProxy(Object targetObject){
        this.targetObject = targetObject;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("执行前");
        Object object =  method.invoke(targetObject,args);
        System.out.println("执行后");
        return object;
    }

    public static void main(String[] args) {

        StudentServiceImpl impl = new StudentServiceImpl();
        StudentProxy studentProxy = new StudentProxy(impl);

        StudentService studentService = (StudentService) Proxy.newProxyInstance(impl.getClass().getClassLoader(),impl.getClass().getInterfaces(),studentProxy);
        studentService.addStudent();
    }
}
